<?php
/**
 * 订单中心逻辑处理
 * User:LLW
 * Date:2020-03-25
 */

namespace app\api\model;

use think\Db;
use think\Model;
use think\response\Json;
use think\Session;

class OrderModel extends Model
{
    protected $table = 'yc_order';

    /**
     * 订单列表
     * @param $state 0:待付款 1:待发货 2:待收货 3:待评价 4:退货中 5:退货成功 6:交易关闭 7交易完成
     * @return json
     */
    public function getList()
    {
        //初始化相关数据
        $uid = Session::get('uid');
        $state = input('state',null);
        if(!empty($state)){
            if($state < 4){
                $where['or_state'] = $state;
            }
        }
        $where['or_uid'] = $uid;
        $list = $this->alias('o')
            ->join('yc_goods g','o.or_gid=g.go_id')
            ->where($where)
            ->field('o.or_nums,o.or_state,o.or_pay,o.or_type,g.go_weight,g.go_thumb,g.go_name,o.or_id,g.go_id,g.go_price')
            ->order('or_xtime desc')->select();
        foreach ($list as $k=>$v){
            $list[$k]['go_weight'] = $v['or_nums'] * $v['go_weight'];//总重量
            if($v['or_type'] == 0){
                $list[$k]['or_type'] = '抢先兑';
            }else{
                $list[$k]['or_type'] = '特价超市';
            }
            $list[$k]['state'] = $this->getState($v['or_state']);
        }
        AjaxReturn(1,'成功',$list);
    }

    /**
     * 订单详情
     * @param $id 订单编号
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getFind()
    {
        $id = input('post.id');
        $order = $this->alias('o')
            ->join('yc_goods g','o.or_gid=g.go_id','left')
            ->field('o.*,g.go_name,g.go_thumb,g.go_price,g.go_score')
            ->where('or_id',$id)
            ->find();
        if(empty($order)){
            AjaxReturn(-1,'订单信息错误');
        }
        if($order['or_type'] == 0){
            $order['or_type'] = '抢先兑';
        }else{
            $order['or_type'] = '特价超市';
        }
        $order['or_expr'] = unserialize($order['or_expr']);
        $order['or_state'] = $this->getState($order['or_state']);
        $order['or_xtime'] = date('Y-m-d H:i:s',$order['or_xtime']);
        AjaxReturn(1,'ok',$order);
    }

    /**
     * 改变订单
     * @param $id 订单id
     * @param $type 1:订单删除 2:取消订单 3:确认收货
     * @return Json
     */
    public function getDel()
    {
        $id = input('get.id');
        $type = input('get.type');
        $uid = Session::get('uid');
        $find = $this->where(['or_id'=>$id,'or_uid'=>$uid])->find();
        if(empty($find)){
            AjaxReturn(-1,'订单信息错误');
        }
        if($type == 1){
            $res = $this->where('or_id',$id)->delete();
        }elseif ($type == 2){
            $res = $this->where('or_id',$id)->setField('or_state',6);
        }elseif ($type == 3){
            $res = $this->where('or_id',$id)->setField('or_state',7);
        }
        if($res){
            AjaxReturn(1,'成功');
        }else{
            AjaxReturn(-1,'网络错误');
        }
    }

    /**
     * 评价订单
     * @throws \think\db\exception\DataNotFoundException
     */
    public function postComm()
    {
        $data = input('post.');
        $uid = Session::get('uid');
        $find = $this->where('or_id',$data['oc_oid'])->find();
        if(empty($find)){
            AjaxReturn(-1,'订单信息错误');
        }
        $data['oc_uid'] = $uid;
        $data['oc_time'] = time();
        $res = Db::name('order_evaluation')->insert($data);
        if($res){
            $this->where('or_id',$data['oc_oid'])->setField('or_state',7);
            AjaxReturn(1,'评价成功');
        }else{
            AjaxReturn(-1,'评价失败');
        }
    }

    /**
     * 添加购物车
     * @throws \think\Exception
     */
    public function AddCart()
    {
        //初始化数据
        $data = input('post.');
        $uid = Session::get('uid');
        $ocident = Db::name('order_cart');

        //获取相关数据
        $find = $ocident->where(['oc_gid'=>$data['oc_gid'],'oc_uid'=>$uid])->find();

        //执行操作并返回结果
        if(empty($find)){
            $data['oc_uid'] = $uid;
            $data['oc_time'] = time();
            $res = $ocident->insert($data);
        }else{
            $res = $ocident->where('oc_id',$find['oc_id'])->setInc('oc_num',$data['oc_num']);
        }
        if($res){
            AjaxReturn(1,'添加成功');
        }else{
            AjaxReturn(-1,'添加失败');
        }
    }

    /**
     * 商品结算
     * @param $data['type'] 1：单个商品结算 2：购物车商品结算
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getBuy()
    {
        $data = input('post.');
        $uid = Session::get('uid');
        $adid = input('adid',null);
        $adident = Db::name('address');
        if(empty($adid)){
            $address = $adident->where(['ad_uid'=>$uid,'ad_default'=>1])->find();
        }else{
            $address = $adident->where(['ad_uid'=>$uid,'ad_id'=>$adid])->find();
        }
        if($data['type'] == 1){
            //获取商品的初始数据
            $goods = Db::name('goods')->alias('g')
                ->join('yc_category c','g.go_caid=c.ca_id')
                ->where(['go_id'=>$data['go_id']])
                ->field('g.go_score,g.go_price,g.go_name,g.go_thumb,g.go_weight,c.ca_parent')
                ->find();
            //运费
            $mail = $goods['ca_parent'] == 1 ? tpConfig('Tyun') : tpConfig('Qyun');
            //所需积分
            $integral = $goods['go_score'] * $data['or_nums'];
            //所需现金
            $cash = $goods['go_price'] * $data['or_nums'];
            $goods['ca_parent'] = $goods['ca_parent'] == 1 ? '特价超市' : '抢先兑';
            $arr = array('goods'=>$goods,'address'=>$address,'freight'=>$mail,'score'=>$integral,'cash'=>$cash + $mail);
            AjaxReturn(1,'ok',$arr);
        }else{
            //购物车结算
            $orid = array_filter(explode(',',$data['go_id']));
            $cas = 0;
            $sco = 0;
            foreach ($orid as $v){
                //购物车的相关数据
                $goods = Db::name('order_cart')->alias('o')
                    ->join('yc_goods g','o.oc_gid=g.go_id')
                    ->join('yc_category c','g.go_caid=c.ca_id')
                    ->where(['oc_id'=>$v])
                    ->field('o.oc_num,g.go_score,g.go_price,g.go_name,g.go_thumb,g.go_weight,g.go_id,c.ca_parent')
                    ->find();

                if(!empty($goods)){
                    //订单总积分
                    $integral = $goods['go_score'] * $goods['oc_num'];
                    $sco += $integral;
                    //订单所需金额
                    $cash = $goods['go_price'] * $goods['oc_num'];
                    $cas += $cash;
                    if($goods['ca_parent'] == 1){
                        $Argoods['Te'][] = $goods;
                    }else{
                        $Argoods['Qx'][] = $goods;
                    }
                }
            }
            if(isset($Argoods['Te']) && isset($Argoods['Qx'])){
                $mails =  tpConfig('Tyun') + tpConfig('Qyun');
            }
            $arr = array('goods'=>$Argoods,'address'=>$address,'freight'=>$mails,'score'=>$sco,'cash'=>$cas + $mails);
            AjaxReturn(1,'ok',$arr);
        }
    }







    /******************************** 私有方法 ***************************************/

    /**
     * 判断订单状态
     * @param $state
     * @return string
     */
    public function getState($state)
    {
        if($state == 0){
            $list = '待付款';
        }elseif ($state == 1){
            $list = '待发货';
        }elseif ($state == 2){
            $list = '待收货';
        }elseif ($state == 3){
            $list = '待评价';
        }elseif ($state == 4){
            $list = '退货中';
        }elseif ($state == 5){
            $list = '退货成功';
        }elseif ($state == 6){
            $list = '交易关闭';
        }elseif ($state == 7){
            $list = '交易完成';
        }else{
            $list = '';
        }
        return $list;
    }

    /**
     * 生成待付款订单
     * @param $or_batch 订单编号
     * @param $or_gid  商品id
     * @param $or_nums  购买数量
     * @param $or_mail  运费
     * @param $or_integral  使用积分
     * @param $or_cash  使用现金
     * @param $or_price  总价
     * @param $or_aid  收货地址
     * @return bool
     */
    public function getOrder($or_batch,$or_gid,$or_nums,$or_mail,$or_integral,$or_cash,$or_price,$or_aid)
    {
        //生成订单
        $data['or_batch'] = $or_batch;
        $data['or_gid'] = $or_gid;
        $data['or_uid'] = Session::get('uid');
        $data['or_nums'] = $or_nums;
        $data['or_mail'] = $or_mail;
        $data['or_integral'] = $or_integral;
        $data['or_cash'] = $or_cash;
        $data['or_price'] = $or_price;
        $data['or_aid'] = $data['or_cash'];
        $data['or_aid'] = $or_aid;
        echo "<pre>";
        print_r($data);
        die;
        //执行数据操作
        $orid = $this->insertGetId($data);
        return $orid;
    }
}